package com.dstz.demo.core.manager.impl;

import com.dstz.demo.core.dao.MaterialsUploadDao;
import com.dstz.demo.core.manager.MaterialsUploadManager;
import com.dstz.demo.core.model.Clclb;
import com.dstz.demo.core.model.FindSupplier;
import com.dstz.demo.core.model.Findings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("materialsUploadManager")
public class MaterialsUploadManagerImpl implements MaterialsUploadManager {

    @Resource
    MaterialsUploadDao materialsUploadDao;

    @Override
    public void insertList(List<String> list, String sub) {
        list.stream().filter(s -> s == null);
        HashMap<String, String> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put("clid", list.get(i));
            map.put("dist", sub);
            materialsUploadDao.insertList(map);
            System.out.println("添加成功");
        }
    }

    //审核通过
    @Override
    public void success(String id) {
        materialsUploadDao.update(id);
    }

    //审核不通过
    @Override
    public void fail(String id, String msg) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("msg", msg);
        materialsUploadDao.fail(map);
    }

    //查询材料库中的信息
    @Override
    public List<Clclb> selectOne(String id) {
        List<Clclb> byid = materialsUploadDao.findByid(id);
        return byid;
    }

    //查询服务库中的详细信息
    @Override
    public List<com.dstz.demo.core.model.Service> selectlist(String id) {
        List<com.dstz.demo.core.model.Service> bylist = materialsUploadDao.findBylist(id);
        return bylist;
    }

    //查询调查任务信息
    @Override
    public Map<String, String> scdcfpryb(String id) {
        Map<String, String> scdcfpryb = materialsUploadDao.scdcfpryb(id);
        return scdcfpryb;
    }

    //材料库结果录入
    @Override
    public void addFindings(String id, String materialID, List<FindSupplier> list, String dcyf, String currentUserId) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(dcyf);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //先获取调查内容id
        Map<String, String> stringStringMap = geta_scdcgzfpID(id, materialID);
        String id1 = stringStringMap.get("id");
        String dcrwlx = stringStringMap.get("dcrwlx");
        //如果查询出来有值不需要创建，如果没有值就创建一下
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id1);
        map.put("materialID", materialID);
        Findings findings = materialsUploadDao.selectBydcnrid(map);
        if (findings == null) {
            findings = new Findings();
            findings.setDcnrid(id1);
            findings.setDcrwlx(dcrwlx);
            findings.setDcclid(materialID);
            findings.setJgscrid("1");
            findings.setJgscsj(new Date());
            findings.setDcyf(parse);
            materialsUploadDao.addfindings(findings);
        }
        String id2 = findings.getId();//上传完成之后生成的id
        //添加
        for (int i = 0; i < list.size(); i++) {
            FindSupplier findSupplier = list.get(i);
            findSupplier.setDcjgid(id2);
            //向供应商表中插入数据
            materialsUploadDao.addfindSupplier(findSupplier);
            System.out.println("添加供应商信息材料" + i);
        }
        materialsUploadDao.updateStauts(id);
        System.out.println("修改状态为待审核");

    }

    //服务库结果录入
    @Override
    public void addFindingsService(String id, String ServiceID, List<FindSupplier> list, String dcyf,String currentUserId) {
        Map<String, String> stringStringMap = getgeta_scdcgzfpServcieID(id, ServiceID);
        String id1 = stringStringMap.get("id");
        String dcrwlx = stringStringMap.get("dcrwlx");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id1);
        map.put("ServiceID", ServiceID);
        Findings findings = materialsUploadDao.selectBydcccid(map);
        if (findings == null) {
            findings = new Findings();
            findings.setDcnrid(id1);
            findings.setDcfwid(ServiceID);
            findings.setDcrwlx(dcrwlx);
            findings.setJgscrid("1");
            findings.setJgscsj(new Date());
            Date parse = null;
            try {
                parse = new SimpleDateFormat("yyyy-MM").parse(dcyf);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            findings.setDcyf(parse);
            materialsUploadDao.addfindingsService(findings);
        }
        String id2 = findings.getId();//上传完成之后生成的id
        //添加
        for (int i = 0; i < list.size(); i++) {
            FindSupplier findSupplier = list.get(i);
            findSupplier.setDcjgid(id2);
            //向供应商表中插入数据
            materialsUploadDao.addfindSupplier(findSupplier);
            System.out.println("添加供应商信息服务" + i);
        }
        materialsUploadDao.updateStauts(id);
        System.out.println("修改状态为待审核");
    }

    //查询任务中材料录入的结果
    @Override
    public List<FindSupplier> materialsDetails(String id) {
//        HashMap<String, String> map = new HashMap<>();
////        map.put("id",id);
////        map.put("dcclid",dcclid);
        return materialsUploadDao.materialsDetails(id);
    }

    //查询任务中服务的录入结果
    @Override
    public List<FindSupplier> serviceDetails(String id) {
        return materialsUploadDao.serviceDetails(id);
    }

    //市场调查统计
    @Override
    public List<Map<String,String>> statistics(String time) {

        return null;
    }


    //查询材料录入的结果
    @Override
    public List<FindSupplier> findListFindSupplier(String id, String materialsID) {
        Map<String, String> stringStringMap = geta_scdcgzfpID(id, materialsID);
        if (stringStringMap.get("id") == null && "".equals(stringStringMap.get("id"))) {
            return null;
        }
        //在a_scdcgzfp表中根据材料id和任务id查询出a_scdcgzfp的id
        String id1 = stringStringMap.get("id");
        //根据id1 查询出录入的供应商信息
        return materialsUploadDao.findDcnrID(id1);
    }

    //查询服务录入的结果
    @Override
    public List<FindSupplier> findServiceListFindSupplier(String id, String serviceID) {
        Map<String, String> stringStringMap = getgeta_scdcgzfpServcieID(id, serviceID);
        if (stringStringMap.get("id") == null && "".equals(stringStringMap.get("id"))) {
            return null;
        }
        String id1 = stringStringMap.get("id");
        return materialsUploadDao.findDcnrID(id1);
    }


    //获取服务调查内容id
    public Map<String, String> getgeta_scdcgzfpServcieID(String id, String ServiceID) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("materialID", ServiceID);
        return materialsUploadDao.getServiceID(map);
    }

    //先获取材料调查内容id
    private Map<String, String> geta_scdcgzfpID(String id, String materialID) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("materialID", materialID);
        return materialsUploadDao.getID(map);
    }

    //改变服务
    @Override
    public List<FindSupplier> serviceDetails1(String id ,String fwid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id",id);
        map.put("fwid",fwid);
        return materialsUploadDao.serviceDetails1(map);
    }

    //  改变材料
    @Override
    public List<FindSupplier> materialsDetails1(String id, String clid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id",id);
        map.put("clid",clid);
        return materialsUploadDao.materialsDetails1(map);
    }
}
